package com.liliudong.template.api.admin.controller.sys;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.liliudong.template.biz.service.sys.SysUserService;
import com.liliudong.template.common.R;
import com.liliudong.template.common.request.sys.SysUserLoginRequest;
import com.liliudong.template.common.response.sys.SysUserLoginResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制器
 *
 * @author liliudong
 * @date 2024年12月28日 23:24
 */
@RestController
@RequestMapping("/sys/auth")
@RequiredArgsConstructor
public class SysAuthController {

    private final SysUserService sysUserService;

    /**
     * 登录
     *
     * @return {@link R }<{@link SysUserLoginResponse }>
     */
    @SaIgnore
    @PostMapping("/login")
    public R<SysUserLoginResponse> login(@RequestBody @Valid SysUserLoginRequest sysUserLoginRequest) {
        SysUserLoginResponse response = sysUserService.login(sysUserLoginRequest.getUsername(), sysUserLoginRequest.getPassword());
        return R.ok(response);
    }

    /**
     * 退出登录
     *
     * @return {@link R }<{@link ? }>
     */
    @SaCheckLogin
    @GetMapping("/logout")
    public R<?> logout() {
        StpUtil.logout();
        return R.ok();
    }

}
